﻿using BrnMall.Manager.Domain.Messages;
using BrnMall.Manager.Manager.Messages;
using Newtonsoft.Json;
using QIDong.WeApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeiTap.Restful;

namespace QIDong.WeApp.Controllers
{
    public partial class MessageController : WebApi
    {
        /// <summary>
        /// 获取消息列表
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="pageNumber">第几页</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        public object GetMessageList(int storeId, int pageNumber = 1, int pageSize = 15)
        {
            if (storeId < 1)
                throw new Exception("请输入storeId");
            var dbList = MessagesManager.GetListPage(storeId, pageNumber, pageSize);
            //留言顶部自定义标题
            var DesignModel = CreateDesignManager.GetDesign(storeId);
            object model = "";
            foreach (var item in dbList.Items)
            {
                if (item.IsCheck == 2)
                {
                     model = "";
                }
                else
                {
                    model = new
                    {
                        MessageList = dbList.Items,
                        PageModel = new PageModel(pageSize, pageNumber, (int)dbList.TotalItems),
                        Title = DesignModel == null ? string.Empty : DesignModel.DesignName
                    };
                    
                }
            }
            return model;

        }
        /// <summary>
        /// 添加一条创意留言
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="title">留言标题</param>
        /// <param name="content">内容</param>
        /// <param name="imgs">图片地址,多个用逗号隔开</param>
        /// <returns></returns>
        public bool Insert(int storeId, string title, string content, string imgs = "")
        {
            var model = new MessagesModel()
            {
                
                IsCheck = 0,
                IsDelete = false,
                MsgTitle = title,
                storeId = storeId,
                MsgContent = content,
                Comment = "",
                LinkType = "",
                LinkValue = "",
                MsgCreateTime = DateTime.Now,
                MsgImg = imgs
            };
            return MessagesManager.Insert(model);
        }

        /// <summary>
        /// 获取一条留言消息
        /// </summary>
        /// <param name="msgid">消息ID</param>
        /// <returns></returns>
        public string GetMessage(int msgid)
        {
            if(msgid<1)
                throw new Exception("请输入msgid");
            MessagesModel mm = MessagesManager.GetMsg(msgid);
            var model = JsonConvert.SerializeObject(mm);
            return model;
        }

    }
}